import * as data from "../../api/data";
var dayjs = require('dayjs');

Page({
    data: {
        currentPage:1,
        createTime:[],
        list:[],
        currentPage:1,
        hasNextPage:true,
        total:"",
        form:{
            currentPage:1,
            pageSize:10,
            dataType:"DAY",
            countryCode:"",
            countDate:""
        },
        increasePerson: 0,
        increaseSaleCount: 0,
        saleCount: 0,
        salePerson:0,
        countryList:[],
        countryIndex:'',
        isOpen:false,
        filterCountry:[],
        show:false
    },

    async onLoad(){

        this.set_init_date( this.data.form.dataType );
        
        let codeRes = await data.getCountryList();
        if(codeRes && codeRes.length > 0){
            codeRes.map((el)=>{
                el.label = el.countryName;
                el.value = el.countryCode;
            })
            this.setData({
                countryList:codeRes,
                ['form.countryCode']:codeRes[0].countryCode,
                filterCountry:codeRes
            })
        }
        this.get_order_list();
    },
    set_init_date( dataType ){
        let countDate = [], start, end;
        if(dataType == 'MONTH'){
            start = dayjs().startOf("month").format("YYYY-MM-DD");
            end = dayjs().format("YYYY-MM-DD")
            countDate = [start, end]
        }
        else if(dataType == 'DAY'){
            start = dayjs().format("YYYY-MM-DD");
            end = dayjs().format("YYYY-MM-DD")
            countDate = [start, end]
        }
        this.setData({
            ['form.countDate']:countDate.join(","),
            start:start,
            end:end
        })
    },
    get_order_list(){

        let form = this.data.form;

        data.getCountryReportList( form )
            .then((res)=>{

                let pageInfo = res.pageInfo;
                let list = pageInfo.list;

                this.setData({
                    list:form.currentPage == 1 ? list :[...this.data.list, ...list],
                    hasNextPage:pageInfo.hasNextPage,
                    total:pageInfo.total,
                    increasePerson: res.increasePerson,
                    increaseSaleCount: res.increaseSaleCount,
                    saleCount: res.saleCount,
                    salePerson:res.salePerson
                })
            })
            .catch(()=>{
                this.setData({
                    hasNextPage:false
                })
            })
    },
    get_search_value(e){
      let val = e.detail.value;
      let filter = this.data.countryList.filter((el)=>{
        return el.label.indexOf(val) !== -1;
      });
      this.setData({
        filterCountry:filter
      })
    },
    change_header_data(e){
        this.setData({
            list:[],
            total:"",
            hasNextPage:true,
            ['form.countDate']:e.detail.countDate,
            ['form.dataType']:e.detail.dataType
        });
        this.get_order_list();
    },
    bindCountryChange(e){
        let { value } = e.currentTarget.dataset;
        let countryList = this.data.countryList;
        this.setData({
            countryIndex:countryList.findIndex(el=>el.countryCode == value ),
            ['form.countryCode']:value,
            list:[],
            total:"",
            hasNextPage:true,
            ['form.currentPage']:1,
            isOpen:false
        });
        this.get_order_list();
    },

    open_date_item(){
        this.setData({
          isOpen:true
        })
    },
    onClose(){
      this.setData({
        isOpen:false
      })
    },
    onReachBottom(){
        if(this.data.hasNextPage){
            this.setData({
                ['form.currentPage']: ++this.data.form.currentPage ,
            })
            this.get_order_list();
        }
    }
})
